package com.tbsoft.trail.bean;

import java.util.ArrayList;
import java.util.List;

import org.apache.camel.Exchange;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.tbsoft.core.process.AbstractProcess;
import com.tbsoft.trail.model.MergedOrder;
import com.tbsoft.trail.model.PurchaseOrder;
import com.tbsoft.trail.service.MergedOrderService;
import com.tbsoft.trail.service.OrderMergeService;
import com.tbsoft.trail.service.PurchaseOrderService;
import com.tbsoft.trail.util.BeanToXmlParse;
/**
 * @desc 订单拆分保存bean
 * @author lizhenhua
 *
 */
public class XmlMergeBean extends AbstractProcess {

	private static final Log log = LogFactory.getLog(XmlMergeBean.class);

	private PurchaseOrderService purchaseOrderService;
	private OrderMergeService orderMergeService;
	private MergedOrderService mergedOrderService;

	public OrderMergeService getOrderMergeService() {
		return orderMergeService;
	}

	public void setOrderMergeService(OrderMergeService orderMergeService) {
		this.orderMergeService = orderMergeService;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public MergedOrderService getMergedOrderService() {
		return mergedOrderService;
	}

	public void setMergedOrderService(MergedOrderService mergedOrderService) {
		this.mergedOrderService = mergedOrderService;
	}

	@SuppressWarnings("unchecked")
	@Override
	public void execute(Exchange exchange) throws Exception {
		log.info("进入XmlMergeBean");
		List<Integer> keys = exchange.getIn().getBody(List.class);
		for (Integer key : keys) {
			log.info("key:" + key);
		}
		// 从表中取出订单
		List<PurchaseOrder> orderLst = new ArrayList<PurchaseOrder>();
		for (Integer key : keys) {
			PurchaseOrder order = this.getPurchaseOrderService().getOrder(key);
			orderLst.add(order);
		}

		MergedOrder mergedOrder = this.getOrderMergeService().mergeOrder(
				orderLst);
		log.info("合并对象为：" + mergedOrder);
		// 保存对象到数据库
		getMergedOrderService().saveMergedOrder(mergedOrder);
		// 把对象转换为报文
		BeanToXmlParse parse = BeanToXmlParse.getInstance();
		parse.putArgs("MergedOrder", mergedOrder, null);
		String xml = parse.parse("vm/MergedOrder.vm");
		log.info("得到的合并报文为："+xml);
		exchange.getOut().setBody(xml);
	}

}
